<template>
  <div class="form-container">
    <el-form inline :model="form" ref="refForm" class="form-line" label-width="70">
      <el-form-item label="使用范围" prop="useScope">
        <el-select v-model="form.useScope" placeholder="使用范围">
          <el-option label="攸诺城配" value="1"></el-option>
          <el-option label="攸诺运管" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="协议名称" prop="title">
        <el-input placeholder="协议名称" v-model="form.title"></el-input>
      </el-form-item>
      <!-- <el-form-item label="协议编号">
        <el-input placeholder="协议编号"></el-input>
      </el-form-item> -->
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(refForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="getProtocolList">查询</el-button>
      <el-button type="primary" @click="handleAdd">添加协议</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      style="width: 100%"
      :data="dataList"
    >
      <el-table-column fixed prop="code" label="协议编号" />
      <el-table-column prop="title" label="协议名称" />
      <el-table-column prop="useScope" label="使用范围">
        <template #default="scope">
          {{ scope.row.useScope == "1" ? "攸诺城配" : "攸诺运管" }}
        </template>
      </el-table-column>
      <el-table-column prop="version" label="协议版本号" />
      <el-table-column prop="updateTime" label="发布时间" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="status" label="上架状态">
        <template #default="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="2" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-popover placement="bottom" :width="40" trigger="click">
            <template #reference>
              <el-button link type="primary" size="small" @click="scope.row.id"> 更多操作 </el-button>
            </template>
            <div style="text-align: center">
              <el-button link type="primary" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
            </div>
            <div style="text-align: center">
              <el-button link type="primary" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
            </div>
            <div style="text-align: center"><el-button link type="primary" size="small">协议更新</el-button></div>
            <div style="text-align: center"><el-button link type="primary" size="small">查看详情</el-button></div>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        @change="getProtocolList"
        layout="prev, pager, next"
        :total="50"
        :page-size="form.pageSize"
        v-model:current-page="form.pageNum"
      />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { Agreement } from "@/api/interface/province/base";
import { deleteProtocolAPI, getProtocolListAPI } from "@/api/modules/province/baseAPI";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const form = ref<{
  useScope: string;
  title: string;
  pageSize: number;
  pageNum: number;
}>({
  useScope: "",
  title: "",
  pageSize: 10,
  pageNum: 1
});
const refForm = ref<FormInstance>();
const dataList = ref<Agreement.Row[]>([]);
const handleReset = (formEl: FormInstance) => {
  formEl.resetFields();
  getProtocolList();
};
const handleDelete = (id: string) => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deleteProtocol(id);
    })
    .catch(() => {
      ElMessage.error("已取消删除");
    });
};
const handleAdd = () => {
  router.push({ path: "/provinceLevel/proBasic/proAgreement/add" });
};
const handleEdit = (id: string) => {
  router.push({
    path: "/provinceLevel/proBasic/proAgreement/add",
    query: { id }
  });
};
const getProtocolList = async () => {
  const { code, rows } = await getProtocolListAPI(form.value);
  if (code == "200" && rows) {
    dataList.value = rows;
  }
};
const deleteProtocol = async (id: string) => {
  const { code } = await deleteProtocolAPI(id);
  if (code == "200") {
    ElMessage.success("删除成功");
    getProtocolList();
  } else {
    ElMessage.error("删除失败");
  }
};
onMounted(() => {
  getProtocolList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
    ::v-deep .el-button > span {
      font-size: 13px;
    }
  }
  .el-form {
    display: inline-block;
    ::v-deep .el-form-item__label {
      font-size: 13px;
    }
  }
  .form-line {
    .el-form-item {
      width: 260px;
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
</style>
